package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public List<AgreementItemVo> listItem(Long userId) {

        UserInfo userInfo = userInfoService.getOne(new LambdaQueryWrapper<>(UserInfo.class).eq(BaseEntity::getId, userId));
        String phone = userInfo.getPhone();
        List<LeaseAgreement> leaseAgreements = baseMapper.selectList(new LambdaQueryWrapper<>(LeaseAgreement.class).eq(LeaseAgreement::getPhone, phone));

        List<AgreementItemVo> agreementItemVoList = leaseAgreements.stream().map(leaseAgreement -> {
            AgreementItemVo agreementItemVo = new AgreementItemVo();
            agreementItemVo.setLeaseStatus(leaseAgreement.getStatus());
            agreementItemVo.setIsRenewable(true);
            BeanUtils.copyProperties(leaseAgreement, agreementItemVo);
            //查询房间编号
            RoomInfo roomInfo = roomInfoMapper.selectOne(new LambdaQueryWrapper<>(RoomInfo.class).eq(RoomInfo::getRoomNumber,leaseAgreement.getRoomId()));
            agreementItemVo.setRoomNumber(roomInfo.getRoomNumber());
            //图片信息
            List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<>(GraphInfo.class)
                    .eq(GraphInfo::getItemId, roomInfo.getId())
                    .eq(GraphInfo::getItemType, 2)
            );

            List<GraphVo> graphVoList = new ArrayList<>();
            for (GraphInfo graphInfo : graphInfos) {
                GraphVo graphVo = new GraphVo();
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                graphVoList.add(graphVo);
            }

            agreementItemVo.setRoomGraphVoList(graphVoList);
            //查询公寓名称
            ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
            agreementItemVo.setApartmentName(apartmentInfo.getName());

            return agreementItemVo;
        }).collect(Collectors.toList());

        return agreementItemVoList;
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {
        return baseMapper.selectDetailById(id);
    }
}




